package Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import Model.Shebei;
import Dao.ShebeiMapper;
import Service.ShebeiService;
import utils.ReturnData;
import utils.ReturnDataForLayui;
import utils.UpdateOrInsertResultDeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import utils.permission.PageDataResult;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class ShebeiServiceImpl implements ShebeiService {
    @Autowired
    private ShebeiMapper shebeiMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    @Override
    public ReturnDataForLayui getList(Shebei shebei) {
        PageHelper.startPage(shebei.getPage(), shebei.getLimit());
        List<Shebei> list = shebeiMapper.getList(shebei);
        PageInfo<Shebei> info = new PageInfo<>(list);
        return ReturnDataForLayui.success(list, info.getTotal());
    }
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    @Override
    public ReturnDataForLayui getName(Shebei shebei) {
        PageHelper.startPage(shebei.getPage(), shebei.getLimit());
        List<Shebei> list = shebeiMapper.getName(shebei);
        PageInfo<Shebei> info = new PageInfo<>(list);
        return ReturnDataForLayui.success(list, info.getTotal());
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ReturnData add(Shebei shebei) {
        int i = shebeiMapper.add(shebei);
        return UpdateOrInsertResultDeal.dealWith(i);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ReturnData delete(Shebei shebei) {
        int i = shebeiMapper.delete(shebei);
        return UpdateOrInsertResultDeal.dealWith(i);
    }


    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ReturnData update(Shebei shebei) {
        int i = shebeiMapper.update(shebei);
        return UpdateOrInsertResultDeal.dealWith(i);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ReturnData updateBeizhu(Shebei shebei) {
        int i = shebeiMapper.updateBeizhu(shebei);
        return UpdateOrInsertResultDeal.dealWith(i);
    }


    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ReturnData updateState(Shebei shebei) {
        int i = shebeiMapper.updateState(shebei);
        return UpdateOrInsertResultDeal.dealWith(i);
    }

    @Override
    public String setShebei(Shebei shebei, HttpSession session) {
        if (shebei.getId()!= null) {
            //!=null说明是编辑
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            shebei.setUpdateTime(timestamp);
            this.shebeiMapper.updateShebeiById(shebei);
        } else {
            // 新增用户
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            shebei.setCreateTime(timestamp);
            shebei.setUpdateTime(timestamp);
            this.shebeiMapper.insertShebei(shebei);
        }
        return "ok";
    }

    @Override
    public Shebei getShebei(Integer id) {
        return shebeiMapper.getShebei(id);
    }


    @Override
    public PageDataResult getShebeis(Shebei shebeiSearch, int page, int limit) {
        System.out.println("page:" + page + "limit:" + limit);

        PageDataResult pdr = new PageDataResult();
        System.out.println(pdr.toString());
        PageHelper.startPage(page, limit);
        List<Shebei> devList = shebeiMapper.getShebeis(shebeiSearch);
        // 获取分页查询后的数据
        PageInfo<Shebei> pageInfo = new PageInfo<>(devList);
        // 设置获取到的总记录数total：
        pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
        pdr.setList(devList);
        System.out.println(pdr.toString());
        return pdr;
    }

    @Override
    public String delShebei(Integer id) {
        return this.shebeiMapper.delShbei(id) == 1 ? "ok"
                : "删除失败，请您稍后再试";
    }
}



